/**
 * Copyright (c) 2019 AsHacker All rights reserved.
 * <p>
 * http://www.ashacker.com
 * <p>
 * 版权所有，侵权必究！
 */

package io.renren.lucky;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.MapUtils;
import io.renren.modules.lucky.dao.*;
import io.renren.modules.lucky.entity.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.*;

/**
 * 测试商品信息
 *
 * @author Chen chenchen@ashacker.com
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class ActivityTestService {

    @Autowired
    private AppGoodsInfoDao appGoodsInfoDao;

    @Autowired
    private AppGoodsCommentDao appGoodsCommentDao;

    @Autowired
    private AppGoodsAddrDao appGoodsAddrDao;

    @Autowired
    private AppSysNoticeDao appSysNoticeDao;

    @Autowired
    private AppUserAddrDao appUserAddrDao;

    @Test
    public void queryByFinanceId() {
        try {
            AppGoodsInfoEntity infoEntity = new AppGoodsInfoEntity();
            Date date = new Date();
            //1、查询未开始产品flag=2——》flag=3
            int num = appGoodsInfoDao.selectCount(new QueryWrapper<AppGoodsInfoEntity>().eq("goods_flag", 2)
                    .le("start_time", date).ge("end_time", date));
            if (num > 0) {
                infoEntity.setGoodsFlag(3);
                appGoodsInfoDao.update(infoEntity, new QueryWrapper<AppGoodsInfoEntity>().eq("goods_flag", 2)
                        .le("start_time", date).ge("end_time", date));
            }
            //2、查询已开始产品flag=3——》flag=4
            List<AppGoodsInfoEntity> list2 = appGoodsInfoDao.selectList(new QueryWrapper<AppGoodsInfoEntity>().eq("goods_flag", 3)
                    .le("end_time", date));
            if (list2.size() > 0) {
                infoEntity.setGoodsFlag(4);
                appGoodsInfoDao.update(infoEntity, new QueryWrapper<AppGoodsInfoEntity>().eq("goods_flag", 3)
                        .le("end_time", date));
                //winUser
//                if(StringConstants.winnerFlag==0)
//                    winUser(list2);
            }
            //3、查询已创建未上线产品flag=6——》flag=5
            List<AppGoodsInfoEntity> list = appGoodsInfoDao.queryGoodsListByDay(6, "1", "7");
            if (list.size() > 0) {
                String ids = "";
                for (AppGoodsInfoEntity infoEntity1 : list) {
                    ids = infoEntity1.getId() + ",";
                }
                ids = ids.substring(0, ids.length() - 1);
                infoEntity.setGoodsFlag(5);
                appGoodsInfoDao.update(infoEntity, new QueryWrapper<AppGoodsInfoEntity>().eq("goods_flag", 6)
                        .in("id", ids));
            }
            //4、查询明日活动或之后7天准备开始的活动产品flag=5——》flag=2
            List<AppGoodsInfoEntity> list1 = appGoodsInfoDao.queryGoodsListByDay(5, "0", "0");
            if (list1.size() > 0) {
                String ids = "";
                for (AppGoodsInfoEntity infoEntity1 : list1) {
                    ids = infoEntity1.getId() + ",";
                }
                ids = ids.substring(0, ids.length() - 1);
                infoEntity.setGoodsFlag(2);
                appGoodsInfoDao.update(infoEntity, new QueryWrapper<AppGoodsInfoEntity>().eq("goods_flag", 5)
                        .in("id", ids));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void winUser() {
        List<AppGoodsCommentEntity> commentEntityList = new ArrayList<AppGoodsCommentEntity>();
        Map<Integer, Double> map = new HashMap<>();
        commentEntityList = appGoodsCommentDao.selectList(new QueryWrapper<AppGoodsCommentEntity>()
                .eq("comment_flag", 1)
                .eq("comment_type", 2)
                .le("create_time", DateUtils.stringToDate("2020-01-18 20:00:00", DateUtils.DATE_TIME_PATTERN))
                .ge("create_time", DateUtils.stringToDate("2020-01-18 12:00:00", DateUtils.DATE_TIME_PATTERN))
                .orderByAsc("create_time"));
        for (AppGoodsCommentEntity commentEntity : commentEntityList) {
            double price = Double.parseDouble(commentEntity.getCommentContent()) - Double.parseDouble("29.8");
            map.put(commentEntity.getUserId(), price);
        }
        if (!map.isEmpty()) {
            double value = Double.parseDouble(MapUtils.getMinValue(map).toString());
            int userId = 0;
            for (Map.Entry<Integer, Double> m : map.entrySet()) {
                if (m.getValue().equals(value)) {
                    userId = m.getKey();
                    break;
                }
            }
            AppUserAddrEntity addrEntity = appUserAddrDao.selectOne(new QueryWrapper<AppUserAddrEntity>()
                    .eq("user_id", userId).eq("addr_flag", 1).eq("addr_default", 1));
            if (addrEntity == null || addrEntity.equals("")) {
                AppSysNoticeEntity noticeEntity = new AppSysNoticeEntity();
                noticeEntity.setUserId(userId);
                noticeEntity.setCreateTime(new Date());
                noticeEntity.setNoticeFlag(1);
                noticeEntity.setNoticeTitle("中奖了");
                noticeEntity.setNoticeContent("本期商品，您已猜中，请填写收货地址，我们才可以进行发货。");
                appSysNoticeDao.insert(noticeEntity);
            }
            AppGoodsAddrEntity addrEntity1 = new AppGoodsAddrEntity();
            addrEntity1.setUserId(userId);
            addrEntity1.setAddrId(addrEntity.getId());
            Date date = new Date();
            addrEntity1.setCreateTime(date);
            addrEntity1.setUpdateTime(date);
            addrEntity1.setGoodsId(35);
            appGoodsAddrDao.insert(addrEntity1);
        }
    }

}